package moe.yakusoku.ll3thablumdraw.controller;

import moe.yakusoku.ll3thablumdraw.bean.DrawResult;
import moe.yakusoku.ll3thablumdraw.service.DrawService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


@RestController
public class DrawController {
    @Autowired
    DrawService service;

    @Value("${draw.do.time}")
    private String doTime;

    static DrawResult result;

    @GetMapping("/llapi/draw")
    public Object draw(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date;

        try {
             date = simpleDateFormat.parse(doTime);
        } catch (ParseException e) {
            return "server error!";
        }

        if(date.getTime() > new Date().getTime()){
            return service.def();
        }else{
            if(null == result) return result = service.draw();
            else return result;
        }
    }

}
